<template>
  <div>
    <el-table
      ref="tables"
      v-loading="loading"
      :default-expand-all="true"
      border
      stripe
      @select-all="selectAll"
      @select="select"
      :data="dataList"
      style="width: 100%;height: 2200px !important;"
      row-key="id"
      :tree-props="{ children: 'children' }"
      @row-click="rowClick"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="评审项目" align="left" width="330" sortable>
        <template slot-scope="scope">
          <i
            v-if="scope.row.children == undefined"
            style="font-size: 12px"
            class="el-icon-arrow-right"
          ></i
          >{{ scope.row.reviewProject }}
        </template>
      </el-table-column>
      <el-table-column label="评审类型" align="left" prop="reviewClassify" />
      <el-table-column label="是否控制点" align="left" prop="isControlPoint" />
    </el-table>
    <div class="always_handerOne1">
      <custom-buttons @back="dialogQx" @confirm="dialogQr" :confirmText="title"></custom-buttons>
    </div>
  </div>
</template>

<script>
import CustomButtons from "@/components/CustomButton/CustomButton.vue";
export default {
  props: {
    menuList: {
      type: Array,
      default: "取消",
    },
  },
  components:{CustomButtons},
  data() {
    return {
      dataList: [],
      ids: [],
      title:'保存',
      loading:false,
      idsList:[],
    };
  },
  created() {
    this.dataList = this.menuList;
  },
  methods: {
    dialogQx() {
        this.$emit('dialogCancel')
    },
    dialogQr() {
        this.$emit("dialogTrue", this.idsList);
    },
    rowClick(row) {
      this.$emit("rowClick", row);
    },
    selectAll(selection) {
      if (selection.length === this.dataList.length) {
        let rows = [];
        this.getChildren(rows, this.dataList);
        rows.forEach((row) => {
          this.$refs.tables.toggleRowSelection(row, true);
        });
      } else {
        let rows = [];
        this.getChildren(rows, this.dataList);
        rows.forEach((row) => {
          this.$refs.tables.toggleRowSelection(row, false);
        });
      }
    },
    select(selection, row) {
      let rows = [];
      this.getChildren(rows, row.children);
      rows.forEach((childRow) => {
        this.$refs.tables.toggleRowSelection(childRow);
      });
    },
    handleSelectionChange(selection) {
        this.idsList = selection
      this.ids = []; // 重置选中的ids
    //   this.idsList = []
      this.flattenSelection(selection); // 调用递归函数
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    flattenSelection(nodes) {
       nodes.forEach((node) => {
        this.ids.push(node.id); // 收集当前节点的id
        if (node.children && node.children.length) {
          this.flattenSelection(node.children); // 递归处理子节点
        }
      });
    },
    getChildren(rows, children) {
      if (children) {
        children.forEach((child) => {
          rows.push(child);
          this.getChildren(rows, child.children);
        });
      }
    },
  },
};
</script>
<style scoped>
.always_handerOne1 {
  height: auto;
  background-color: #ffffff;
  /* border-radius: 14px; */
  padding-top: 28px;
}
.el-tree__empty-block{
  height:550px !important;
}
</style>
